package com.buildings.adapter;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.buildings.R;

public class TableListObjAdapter extends RecyclerView.Adapter<TableListObjAdapter.ViewHolder> {
    private Activity activity;
    private String[] titleArr;
    //文本类型 1：输入框 2：选择框
    private int[] arrType;
    //是否必填
    private boolean[] isBoolean;

    public TableListObjAdapter(Activity activity, String[] titleArr
            , int[] arrType, boolean[] isBoolean) {
        this.activity = activity;
        this.titleArr = titleArr;
        this.arrType = arrType;
        this.isBoolean = isBoolean;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_table_list, viewGroup, false);
        TableListObjAdapter.ViewHolder holder = new TableListObjAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (1 == arrType[position]) {
            holder.llSelect.setVisibility(View.GONE);
            holder.etInput.setVisibility(View.VISIBLE);
        } else {
            holder.llSelect.setVisibility(View.VISIBLE);
            holder.etInput.setVisibility(View.GONE);
        }
        holder.tvTitle.setText(titleArr[position]);
        if (isBoolean[position]) {
            holder.tvMust.setVisibility(View.VISIBLE);
        } else {
            holder.tvMust.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return titleArr.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        //标题
        TextView tvTitle;
        //必填符号
        TextView tvMust;
        //数据输入框
        EditText etInput;
        //选择框父布局
        LinearLayout llSelect;
        //选择文本框
        EditText etSelect;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvMust = itemView.findViewById(R.id.tv_must);
            etInput = itemView.findViewById(R.id.et_input);
            llSelect = itemView.findViewById(R.id.ll_select);
            etSelect = itemView.findViewById(R.id.et_select);
        }
    }
}
